﻿
//时间格式化
Date.prototype.format = function (format) //author: meizz 
{
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

function StrToDate(JSONDateString) {
    var date = new Date(parseInt(JSONDateString.replace("/Date(", "").replace(")/", ""), 10));
    return date.format("yyyy-MM-dd hh:mm:ss");
}

$("#txtInputDate").datepicker({
    "dateFormat": "yy-mm-dd"
});

//删除事件
function delClick(obj) {
    if (confirm("确认要删除？")) {
        var delid = $(obj).attr("id");
        $.ajax({
            type: "post",
            url: "/ashx/StartStorage.ashx",
            data: { delbarid: $(obj).attr("alt") },
            success: function (msg) {
                alert("删除成功");
                $(obj).parent().parent().remove();
            }
        });
    }
}

//编辑事件
function editData(obj) {
    $(obj).parent().parent().parent().children("tr").css("color", "black");
    $(obj).parent().parent().parent().children("tr").attr("id", false);
    $("#lblNo").html($(obj).parent().parent().children("td").eq(0).html());

//        $("#txtgc").val($(obj).parent().parent().children("td").eq(1).html());
//        $("#txtgn").val($(obj).parent().parent().children("td").eq(2).html());
        $("#txtgc").val($(obj).parent().parent().children("td").eq(3).html());
        $("#txtgn").val($(obj).parent().parent().children("td").eq(4).html());
        $("#hidgdid").val($(obj).parent().parent().children("td").eq(4).attr("id"));
        $("#txtgwt").val($(obj).parent().parent().children("td").eq(5).html());
        $("#txttare1").val($(obj).parent().parent().children("td").eq(6).html());
        $("#txttare2").val($(obj).parent().parent().children("td").eq(7).html());
        $("#txtnwt").html($(obj).parent().parent().children("td").eq(8).html());
        $("#txtqty").val($(obj).parent().parent().children("td").eq(9).html());
        $("#txtsid").val($(obj).parent().parent().children("td").eq(10).html());
        $(".HidPostion").val($(obj).parent().parent().children("td").eq(10).attr("id"));
        $("#lblbarcode").html($(obj).parent().parent().children("td").eq(11).html());
        $("#txthw").val($(obj).parent().parent().children("td").eq(12).html());
        $("#lblHwType").html($(obj).parent().parent().children("td").eq(13).html());
        $("#txtrdh").val($(obj).parent().parent().children("td").eq(14).html());
        $("#txtboxno").val($(obj).parent().parent().children("td").eq(15).html());
        $("#txtboxcount").val($(obj).parent().parent().children("td").eq(16).html());
        $("#txtremark").val($(obj).parent().parent().children("td").eq(17).html());
        $("#hidbarid").val($(obj).attr("alt"));
        $("#btnSaveBarcode").val("结存修改");
        $(obj).parent().parent().attr("id", "tr_editid");
 
    $(obj).parent().parent().css("color", "red");
}


$(document).ready(function () {
    //物品编码选取
    $("#txtgc").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/Autocomplete.ashx",
                data: { "goods": $("#txtgc").val(), "type": "PD" },
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label + " " + item.value,
                            value: item.value,
                            lab: item.label,
                            goodsid: item.goodsid
                        }
                    }));
                },
                error: function (result, status) {
                    alert('请输入正确的物品编码');
                }
            })

        },
        select: function (event, ui) {
            $("#txtgc").val(ui.item.lab);
            $("#txtgn").val(ui.item.value);
            $("#hidgdid").val(ui.item.goodsid);
            return false;
        },

        minLength: "1"
    });


    //库位下拉选择根据处理地做筛选
    $("#txtsid").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/BasicStorage.ashx",
                data: {
                    "word": $("#txtsid").val()
                             , "graphic": escape($("#lblcity_graphic").html())
                             , "belong": escape($("#lblcity_belong").html())
                },
                dataType: "json",
                success: function (data) {
                    response(data);
                },
                error: function (result, status) {
                    alert('请输入正确的库位');
                }
            })

        },
        select: function (event, ui) {
            $("#txtsid").val(ui.item.label);
            $(".HidPostion").val(ui.item.value);
            return false;
        },

        minLength: "1"
    });


    //入库危废下拉选择
    $("#txthw").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/Autocomplete.ashx",
                data: { "goods": $("#txthw").val(), "type": "hw" },
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label + " " + item.value,
                            value: item.value,
                            lab: item.label,
                            goodsid: item.goodsid
                        }
                    }));
                },
                error: function (result, status) {
                    alert('请输入正确的危废编码');
                }
            })

        },
        select: function (event, ui) {
            $("#txthw").val(ui.item.lab);
            $("#lblHwType").html(ui.item.value);
            $("#hiduHwid").val(ui.item.goodsid);
            return false;
        },

        minLength: "1"
    });


    //净重计算
    $("#txttare2,#txtgwt,#txttare1").blur(function () {
        if ($("#txttare1").val() != "" && $("#txtgwt").val() != "" && $("#txttare2").val() != "") {
            var gwt = parseFloat($("#txtgwt").val());
            var t1 = parseFloat($("#txttare1").val());
            var t2 = parseFloat($("#txttare2").val());
            var nwt = (gwt - t1 - t2).toFixed(2);
            $("#txtnwt").html(nwt);
        }
    });

    //为空验证
    function CheckInput() {
        var strErr = "";
        if ($("#txtgc").val() == "") strErr += "物品编码不能为空！\n";
        if ($("#txtgn").val() == "") strErr += "物品名称不能为空！\n";
        if ($("#txtgwt").val() == "" || isNaN($("#txtgwt").val())) strErr += "毛重必须输入数字！\n";
        if ($("#txtqty").val() == "" || isNaN($("#txtqty").val())) strErr += "数量必须输入数字！\n";
        if ($("#txttare1").val() == "" || isNaN($("#txttare1").val())) strErr += "皮重1必须输入数字！\n";
        if ($("#txttare2").val() == "" || isNaN($("#txttare2").val())) strErr += "皮重2必须输入数字！\n";
        if ($("#txtsid").val() == "" || $(".HidPostion").val() == "0" || $(".HidPostion").val() == "") strErr += "必须选择合法库位！\n";
        return strErr;
    }

    var timer = null; function do_click(event) {
        clearTimeout(timer); // 这里加一句是为了兼容 Gecko 的浏览器 /
        if (event.detail == 2)
            return;
        // 同上句的作用 
        timer = setTimeout(function () {
            // click 事件的处理 
        }, 300);
    }



  // $("#btnSaveBarcode").dblclick()
  //  {
 //       clearTimeout(timer);
  //  }

    //插入操作
    $("#btnSaveBarcode").click(function () {
        InsertBar();
    });

    function InsertBar() {
        //插入验证
        var strErr = CheckInput();
        if (strErr != "") {
            $("#txtgc").focus();
            alert(strErr);
            return;
        } else {
            checkGoodsName();
        }
    }

    $("input[name='rad_belong']").change(function () {
        //$("#lblcity_belong").html($("input[name='rad_belong'][checked]").val());
        $("#lblcity_belong").html($(this).val());
    });

    if ($(".hidGraphic").val() != "") {
        $("input[name='rad_graphic']").attr("disabled", "disabled");
        //        $("input[name='rad_graphic']").val($(".hidGraphic").val());
        $("input[name='rad_graphic'][type='radio'][value='" + $(".hidGraphic").val() + "']").attr("checked", true);
        $("#lblcity_graphic").html($(".hidGraphic").val());
    }

    $("input[name='rad_graphic']").change(function () {
        $("#lblcity_graphic").html($(this).val());
    });

    //插入条形码事件
    function InsertBarCode() {
        var jston = { "loadid": $(".hiddenloadid").val(),
            "goodsid": $("#hidgdid").val(),
            "gcode": $("#txtgc").val(),
            "gname": $("#txtgn").val(),
            "barvalue": $("#lblbarcode").html(),
            "gwt": $("#txtgwt").val(),
            "nwt": $("#txtnwt").html(),
            "tare1": $("#txttare1").val(),
            "tare2": $("#txttare2").val(),
            "qty": $("#txtqty").val(),
            "storageid": $(".HidPostion").val(),
            "stroageno": $("#txtsid").val(),
            "insid": $(".hiddenInSid").val(),
            "operman": $(".hiddenOper").val(),
            "cjoid": $(".hiddencjoid").val(),
            "bcity": $("#lblcity_belong").html(),
            "gcity": $("#lblcity_graphic").html(),
            "month": $(".dropYear").val() + $(".dropMonth").val(),
            "pddate": $(".txtInputDate").val(),
            "barid": $(".hidbarid").val(),
            "rcode": $("#txthw").val(),
            "rtype": $("#lblHwType").html(),
            "boxno": $("#txtboxno").val(),
            "boxcount": $("#txtboxcount").val(),
            "rdh": $("#txtrdh").val(),
            "remark": $("#txtremark").val()

        };

        $.ajax({
            type: "post",
            url: "/ashx/StartStorage.ashx",
            data: jston,
            success: function (msg) {

                var obj = eval('(' + msg + ')');
                var tr = "";

                if (obj != null && $("#hidbarid").val() != "0") {
                    tr += "<td>" + $("#lblNo").html() + "</td>";
                }
                else {
                    tr += "<td>" + ($('table tr').length - 3) + "</td>";
                }
                tr += "<td>" + obj.barbelong + "</td>";
                tr += "<td>" + obj.bargraphic + "</td>";
                tr += "<td class='barcodevalue'>" + obj.goodscode + "</td>";
                tr += "<td id='" + obj.goodsid + "'>" + obj.goodsname + "</td>";
                tr += "<td>" + obj.gwt + "</td>";
                tr += "<td>" + obj.tare1 + "</td>";
                tr += "<td>" + obj.tare2 + "</td>";
                tr += "<td>" + obj.nwt + "</td>";
                tr += "<td>" + obj.qty + "</td>";
                tr += "<td id='" + obj.storageid + "'>" + obj.stroageNo + "</td>";
                tr += "<td>" + obj.barcodevalue + "</td>";
                tr += "<td>" + obj.rubbishcode + "</td>";
                tr += "<td>" + obj.rubbishtype + "</td>";
                tr += "<td>" + obj.rdh + "</td>";
                tr += "<td>" + obj.packageno + "</td>";
                tr += "<td>" + obj.boxcount + "</td>";
                tr += "<td>" + obj.remark + "</td>";
                tr += "<td><img src='/images/jian.png' alt='" + obj.id + "' width='16' onclick='javascript:delClick(this)'  height='17''/>";
                tr += "<img src='/images/icons/button_update.gif' alt='" + obj.id + "' onclick='javascript:editData(this)' height='17' class='update'/>";
                tr += "&nbsp;&nbsp;<a href='/Storage/BarCodeimg.aspx?code=COAD128A&barCodeID=" + obj.id + "' target='_blank'>条码重打</a></td>";
                if (obj != null && $("#hidbarid").val() != "0") {
                    $("#tr_editid").html(tr);
                    $("#hidbarid").val("0");
                    $("#tr_editid").css("color", "black");
                    $("#lblNo").html("");
                }
                else {
                    tr = "<tr>" + tr + "</tr>";
                    $("#tab_Stoarge").append(tr);
                }
                $("#btnSaveBarcode").val("结存插入");


                document.cookie = "pdid=" + obj.InStrorageID + ";path=/";
                $(".hiddenInSid").val(obj.InStrorageID);
                $(".hiddenloadid").val(obj.loadnumid);
                $(".txtLoadNo").val(obj.loadnumv);
                $(".hiddencjoid").val(obj.cjoid);

                //清空
                $("#txtgc").val("");
                $("#txtgn").val("");
                $("#txtgwt").val("0");
                $("#txtnwt").html("0");
                $("#txttare1").val("0");
                $("#txttare2").val("0");
                $("#txtqty").val("");
                $("#txtpn").val("");
                $("#HidPostion").val("");
                $("#txtsid").val("");
                $("#lblbarcode").html("");
                $("#txthw").val("");
                $("#lblHwType").html("");
                $("#txtboxno").val("");
                $("#txtboxcount").val("");
                $("#txtrdh").val("");
                $("#txtremark").val("");
                window.open("BarCodeImg.aspx?barcodeid=" + obj.id, "_blank");



            }
        });
    }


    //验证称重页面的物品编码，废物编码，库位
    function checkGoodsName() {
        $.ajax({
            type: "post",
            url: "/ashx/IsGoodCode.ashx?",
            data: { goodCodeid: $("#txtgc").val(), sid: $("#txtsid").val(), checkStatus: "1" },
            success: function (msg) {
                switch (msg) {
                    case "-1":
                        alert("不能成功调用！");
                        break;
                    case "0":
                        InsertBarCode();
                        break;
                    case "1":
                        alert("物品编码不合法！");
                        break;
                    case "2":
                        alert("废物编码不合法！");
                        break;
                    case "3":
                        alert("库位编码不合法！");
                        break;
                    default:
                        alert("不正常数据！");
                        break;
                }
            }
        });
    }

});